package de.koller.oceanFramework.plain.util;

import java.util.Arrays;

public class RRaster {

	public final int width, height;
	public final int[] pixels;

	public RRaster( int[] pixels, int width, int height ) {
		this.pixels = pixels;
		this.width = width;
		this.height = height;
	}
	
	public void clear( int color ) {
		Arrays.fill( pixels, color ); 
	}

	public int colorAt( int x, int y ) {
		return getPixelAt( offsetOf( x, y ) );
	}
	
	public int offsetOf( int x, int y ) {
		if( x < 0 || y < 0 || x >= width || y >= height )
			return -1;
		return y * width + x;
	}

	public void setPixel( int color, int x, int y ) {
		int i = offsetOf( x, y );
		if( i < 0 || i >= pixels.length )
			return;
		setPixelAt( i, color );
	}

	public int xFromOffset( int offset ) {
		int ax = offset - ( offset / width ) * width;
		return ax;
	}

	public float yFromOffset( int offset ) {
		int ay = offset / width;
		return ay;
	}

	private int getPixelAt( int offset ) {
		return pixels[ offset ];
	}

	private void setPixelAt( int offset, int color ) {
		pixels[ offset ] = color;
	}
	
}
